package com.key.win.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.key.win.base.page.MybatisPageServiceTemplate;
import com.key.win.base.web.PageRequest;
import com.key.win.base.web.PageResult;
import com.key.win.demo.dao.MybatiesSqlTemplateDao;
import com.key.win.demo.dao.MybatiesTemplateDao;
import com.key.win.demo.model.MybatiesTemplate;
import com.key.win.demo.service.MybatiesTemplateService;
import com.key.win.demo.vo.UserOrganVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MybatiesTemplateServiceImpl extends ServiceImpl<MybatiesTemplateDao, MybatiesTemplate> implements MybatiesTemplateService {

    private static final Logger log = LoggerFactory.getLogger(MybatiesTemplateServiceImpl.class);
    @Autowired
    private MybatiesSqlTemplateDao mybatiesSqlTemplateDao;


    @Cacheable("getCacheTest")
    public String getCacheTest() {
        log.info("1111");
        return "this is good boy!!";
    }

    @Override
    public List<MybatiesTemplate> getMybatiesTemplateByCondition(String name, String code) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(name)) {
            map.put("name", name);
        }
        if (StringUtils.isNotBlank(code)) {
            map.put("code", code);
        }
        return this.baseMapper.selectByMap(map);
    }

    @Override
    public List<MybatiesTemplate> getMybatiesTemplateByLike(String name) {
        return this.baseMapper.selectList(new QueryWrapper<MybatiesTemplate>().like("name", name));
    }

    public PageResult<MybatiesTemplate> findMybatiesTemplateByPaged(PageRequest<MybatiesTemplate> pageRequest) {
        MybatisPageServiceTemplate<MybatiesTemplate, MybatiesTemplate> page = new MybatisPageServiceTemplate<MybatiesTemplate, MybatiesTemplate>(this.baseMapper) {
            @Override
            protected AbstractWrapper constructWrapper(MybatiesTemplate mybatiesTemplate) {
                LambdaQueryWrapper<MybatiesTemplate> lqw = new LambdaQueryWrapper<MybatiesTemplate>();
                if (mybatiesTemplate != null && StringUtils.isNotBlank(mybatiesTemplate.getName())) {
                    lqw.like(MybatiesTemplate::getName, mybatiesTemplate.getName() == null ? "" : mybatiesTemplate.getName());
                }
                if (mybatiesTemplate != null && StringUtils.isNotBlank(mybatiesTemplate.getCode())) {
                    lqw.like(MybatiesTemplate::getCode, mybatiesTemplate.getCode() == null ? "" : mybatiesTemplate.getCode().toUpperCase());
                }

                lqw.orderByDesc(MybatiesTemplate::getCreateDate);
                return lqw;
            }
        };
        return page.doPagingQuery(pageRequest);
    }

    public PageResult<UserOrganVo> getUserOrganByPaged(PageRequest<UserOrganVo> pageRequest) {
        MybatisPageServiceTemplate<UserOrganVo, UserOrganVo> query = new MybatisPageServiceTemplate<UserOrganVo, UserOrganVo>(mybatiesSqlTemplateDao) {
            @Override
            protected AbstractWrapper constructWrapper(UserOrganVo userOrganVo) {
//                QueryWrapper<UserOrganVo> queryWrapper = new QueryWrapper<UserOrganVo>();
//                if (userOrganVo != null) {
//                    if (StringUtils.isNotBlank(userOrganVo.getUserName())) {
//                        queryWrapper.eq("user_name", userOrganVo.getUserName());
//                    }
//                    if (StringUtils.isNotBlank(userOrganVo.getOrganName())) {
//                        queryWrapper.eq("organ_name", userOrganVo.getOrganName());
//                    }
//                }
//                return queryWrapper;
                LambdaQueryWrapper<UserOrganVo> lqw = new LambdaQueryWrapper<UserOrganVo>();
                if (userOrganVo != null) {
                    if (StringUtils.isNotBlank(userOrganVo.getUserName())) {
                        lqw.eq(UserOrganVo::getUserName, userOrganVo.getUserName());
                    }
                    if (StringUtils.isNotBlank(userOrganVo.getOrganName())) {
                        lqw.eq(UserOrganVo::getOrganName, userOrganVo.getOrganName());
                    }
                }
                return lqw;
            }

            @Override
            protected String constructNativeSql() {
                return "SELECT * FROM ( SELECT u.user_name ,o.`name` as organ_name FROM sys_user u INNER JOIN sys_user_organ uo on u.id = uo.user_id INNER JOIN sys_organ o on uo.organ_id = o.id ) user_organ";
            }
        };
        return query.doPagingQuery(pageRequest);
    }
}
